<?php

namespace App\Models;

use Tymon\JWTAuth\Contracts\JWTSubject;
use Spatie\Permission\Traits\HasRoles;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable implements JWTSubject
{
    use Notifiable, HasRoles, SoftDeletes;

    protected $guard_name = 'api';

    protected $dates = ['deleted_at'];


    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'password','section_id', 'branch_id', 'role_id', 'status','login_ip'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * Get the identifier that will be stored in the subject claim of the JWT.
     *
     * @return mixed
     */
    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    /**
     * Return a key value array, containing any custom claims to be added to the JWT.
     *
     * @return array
     */
    public function getJWTCustomClaims()
    {
        return [];
    }

    public function getBranchIdAttribute($value){
        if($value == 0){
            return Null;
        }else{
            return $value;
        }
    }

    public function phones(){
        return $this->hasMany(Phone::class);
    }

    public function fsUserTasks(){
        return $this->hasMany(FsUserTask::class,'carry_user_id');
    }

    public function fsUsers(){
        $db1 = env('DB_DATABASE');
        return $this->belongsToMany(FsUser::class,"{$db1}.fs_user_tasks",'carry_user_id','fs_user_id');
    }
}
